<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/6/6
 * Time: 16:16
 */

namespace app\zyhsapi\controller\v1;

use app\common\lib\Util;
use app\zyhsapi\model\ZyhsAppVersion;
use app\zyhsmarketing\service\MarketingCallback;
use think\response\Json;

/**
 * @title App版本更新接口，客户端根据version_code进行是否升级的判断
 * @desc 【附带 控制星盘数据来源为 客户端本地获取或服务器获取 支付方式控制】
 */
class App
{
    /**
     * @title 获取app版本更新信息,根据app型号和版本,若开启强制更新则返回渠道包号
     * @method GET
     * @desc 是否有新版本发布 || 星盘获取方式有变化
     * @return Json
     * @throws \Exception
     */
    public function index()
    {
        $headers = request()->header();
        $version_code = input('param.version-code');
        $channel = input('channel/s', '');
        /**
         * 获取当前客户端的最新版本的更新信息
         */
        $AppVersion = (new ZyhsAppVersion())->getVersionData(['type' => $headers['app-type'], 'version' => ($headers['version'] ?? '')]);
        if ($AppVersion)
        {
            $array = [
                'version' => (int)$AppVersion['version'],
                'version_name' => $AppVersion['version_name'],
                'update_content' => $AppVersion['update_content'],
                'force' => (int)$AppVersion['force'],
                'logo_type' => (int)$AppVersion['logo_type'],
            ];
            $isForceLogin = 0;
            if ($AppVersion['version'] == $version_code)
            {
                $extend = json_decode($AppVersion['extend'], true);
                $forceLoginChannel = $extend[$headers['app-type']]['force_reg_channel'] ?? [];
                $channel = $headers['app-type'] === 'ios' ? 'ios' : $channel;
                if (!in_array($channel, ['ios', 'huawei', 'xiaomi', 'OPPO', 'VIVO', 'honor'], true))
                {
                    $channel = 'other';
                }
                if (in_array($channel, $forceLoginChannel, true))
                {
                    $isForceLogin = 1;
                }
            } else
            {
                $nowVersion = (new ZyhsAppVersion())->getVersionInfo($headers['app-type'], $headers['version'] ?? '');
                if ($nowVersion)
                {
                    $forceLoginChannel = $nowVersion['extend'][$headers['app-type']]['force_reg_channel'] ?? [];
                    $channel = $headers['app-type'] === 'ios' ? 'ios' : $channel;
                    if (!in_array($channel, ['ios', 'huawei', 'xiaomi', 'OPPO', 'VIVO', 'honor'], true))
                    {
                        $channel = 'other';
                    }
                    if (in_array($channel, $forceLoginChannel, true))
                    {
                        $isForceLogin = 1;
                    }
                }
            }

            $array['is_force_login'] = $isForceLogin;
            if (strtolower($headers['app-type']) === 'android')
            {
                /**
                 * 判断当前app版本是否需要强制更新,指定更新开启且当前版本包含在内
                 */
                $AppVersionCodeIds = empty($AppVersion['version_force_data']) ? [] : json_decode($AppVersion['version_force_data'], true);
                if ($AppVersion['version_force'] == 1 && in_array($version_code, $AppVersionCodeIds))
                {
                    $array['force'] = 1;
                }
            }
            return show(config('code.success'), '加载成功', $array);
        }
        return show(config('code.error'), 'app更新信息获取失败');
    }

    /**
     * APP 首次激活
     * @return Json
     */
    public function newActive()
    {
        $mobileType = input('mobile_type/d', 0);
        $headers = request()->header();
        if (Util::IsIos())
        {
            $mobileType = 6;
        }
        if (!in_array($mobileType, [6, 5, 4, 3, 2, 0], true))
        {
            return show(config('code.error'), '请求异常，请稍后重试～');
        }

        if (Util::IsIos())
        {   // ios

        } else
        {   // 安卓
            $oaid = $headers['oaid'] ?? '';
            // vivo 该位置 激活指标 移除
            if (in_array($mobileType, [5, 4, 2], true))
            {   // 华为=>5 小米=>4 vivo=>3 oppo=>2
                (new MarketingCallback())->uploadActiveData($oaid, 0, 1, $mobileType);
            }
        }
        return show(config('code.success'), '请求成功');
    }
}
